
package todolist.util;

import com.sun.speech.freetts.Voice;
import com.sun.speech.freetts.VoiceManager;

/**
 * For playing Text to Speech audio. Singleton.
 * Please dispose on application exit.
 * Libraries: cmu_time_awb.jar, cmu_us_kal.jar, cmudict04.jar, cmulex.jar, cmutimelex.jar,
 *	en_us.jar, fretts-jsapi10.jar, fretts.jar, mbrola.jar; from: http://freetts.sourceforge.net/
 * Other libraries required: jsapi.jar, jsapi.sh 
 * @author YouLi
 */
public class TTS {
	
	// handle singleton instance
	
	private static TTS _objTTS;
	
	public static TTS getInstance(){
		if (_objTTS == null)
			_objTTS = new TTS();
		return _objTTS;
	}
	
	// member attributes
	
	private Voice _objVoice;
	
	private static final String VOICE_IN_USE = "kevin16";
	
	private TTS() {
		_objVoice = VoiceManager.getInstance().getVoice(VOICE_IN_USE);
		_objVoice.allocate();
	}

	/**
	 * Play audio from first to last.
	 */
	public void say(String[] parrSpeech) {
		if (parrSpeech == null)
			return;
		for (String strSpeech : parrSpeech)
			say(strSpeech);
	}

	/**
	 * Play audio.
	 */
	public void say(String pstrSpeech) {
		if (pstrSpeech == null || pstrSpeech.trim().equals(""))
			return;
		_objVoice.speak(pstrSpeech);
	}

	/**
	 * Clean up resources on application exit.
	 */
	public void dispose() {
		_objVoice.deallocate();
	}
	
}
